package com.yanqu.road.web.mgr;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.servercenter.tabnotice.ChannelTabNotice;
import com.yanqu.road.entity.servercenter.tabnotice.TabNotice;
import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.MyURLEncoderHelper;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

;

/**
 * @Author : Cxp
 * @Time : 2024-12-26
 * @Description : 页签公告Mgr
 */

public class ChannelTabNoticeMgr extends TempMgr {

    private static Map<Integer, List<ChannelTabNotice>> channelTabNoticeMap; // channelId -> ChannelTabNotice

    @Override
    public boolean reloadData() {
        channelTabNoticeMap = ServerBussiness.getChannelTabNoticeMap();
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    /**
     * 根据语言，获取渠道/子渠道 页签公告
     *
     * @param channelId 渠道ID
     * @param appId     子渠道ID，海外无子渠道后台也会传值
     * @param language  语言
     * @return ChannelTabNotice
     */
    public static ChannelTabNotice getChannelTabNotice(int channelId, String appId, String language) {
        if (channelTabNoticeMap.get(channelId) == null) {
            return null;
        }

        if (eChannelType.isMainLandChannel(channelId)) { // 大陆渠道判断appId和语言
            for (ChannelTabNotice channelTabNotice : channelTabNoticeMap.get(channelId)) {
                if (appId.equals(channelTabNotice.getAppId()) && language.equals(channelTabNotice.getLanguage())) {
                    return channelTabNotice;
                }
            }
        } else { // 其它渠道没有appId，仅判断语言
            for (ChannelTabNotice channelTabNotice : channelTabNoticeMap.get(channelId)) {
                if (language.equals(channelTabNotice.getLanguage())) {
                    return channelTabNotice;
                }
            }
        }

        return null;
    }

    /**
     * 发送给客户端的内容
     *
     * @param channelTabNotice 页签公告
     * @return JSONArray
     */
    public static String parseTabNoticeToJsonStr(ChannelTabNotice channelTabNotice) {
        JSONArray jsonArr = new JSONArray();

        if (channelTabNotice != null) {
            for (TabNotice tabNotice : channelTabNotice.getTabNotices()) {
                JSONObject obj = new JSONObject();
                if (tabNotice != null) {
                    obj.put("pictureId", tabNotice.getPictureId());
                    obj.put("gameType", channelTabNotice.getGameType());
                    try {
                        obj.put("content", MyURLEncoderHelper.encode(tabNotice.getContent(), "UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        obj.put("content", tabNotice.getContent());
                    }
                    try {
                        obj.put("title", MyURLEncoderHelper.encode(tabNotice.getTitle(), "UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        obj.put("title", tabNotice.getTitle());
                    }
                    obj.put("announceTime", tabNotice.getAnnounceTime());
                }
                jsonArr.add(obj);
            }
        }

        return jsonArr.toString();
    }
}
